package com.jozen.modelbase.netwoek.util;

import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.jozen.modelbase.R;
import com.jozen.modelbase.netwoek.base.ModuleBaseApplication;


public class MyToastUtil {
    public static Handler mainHandler = new Handler(Looper.getMainLooper());
    private static Toast sToast;
    private static TextView tv_msg;

    public static void show(String msg) {

        mainHandler.post(() -> {
            if (TextUtils.isEmpty(msg)) {
                return;
            }

            if (msg.contains("HTTP 504 Unstatisfiable Request(only-if-cached)") || msg.contains("HTTP 500"))
                return;

            if (sToast != null) {
                sToast.cancel();
                sToast = null;
                tv_msg = null;
            }
            sToast = new Toast(ModuleBaseApplication.getInstance().getApplicationContext());
            tv_msg = new TextView(ModuleBaseApplication.getInstance().getApplicationContext());
            if (tv_msg != null) {
                tv_msg.setBackgroundResource(R.drawable.bg_toast_circle);
                tv_msg.setTextColor(ContextCompat.getColor(ModuleBaseApplication.getInstance().getApplicationContext(), R.color.white));
                tv_msg.setText(msg + "");
                tv_msg.setPadding(25, 10, 25, 10);
            }
            sToast.setView(tv_msg);
            sToast.setGravity(Gravity.TOP, 0, 100);
            sToast.setDuration(Toast.LENGTH_SHORT);
            sToast.show();
        });
    }
}
